package net.golfklubi.sive;

import java.util.List;

import jdave.junit4.JDaveRunner;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.tester.FormTester;
import org.jmock.Expectations;
import org.junit.runner.RunWith;

@RunWith(JDaveRunner.class)
public class SiveHomePageSpec extends SiveSpecification<SiveHomePage> {

    protected CompetitionPersisterAndLoader mockCompetitionPersister = mock(CompetitionPersisterAndLoader.class);

    @SuppressWarnings("unchecked")
    @Override
    protected SiveHomePage newComponent(final String id, final IModel model) {
        return new SiveHomePage() {
            @Override
            protected CompetitionPersisterAndLoader getCompetitionPersisterAndLoader() {
                return mockCompetitionPersister;
            }
        };
    }

    public class Any {

        public SiveHomePage create() {
            checking(new Expectations() {
                {
                    allowing(mockCompetitionPersister).loadFromFile(with(any(String.class)));
                    will(returnValue(TestData.competitionsList()));
                }
            });
            return startComponent();
        }

        public void containsForm() {
            specify(selectFirst(Form.class).from(context).isVisible(), must.equal(true));
        }

        @SuppressWarnings("unchecked")
        public void canBeSubmitted() {
            final FormTester newFormTester = wicket.newFormTester("positionForm");
            newFormTester.setValue("rankingGuess", "2");
            newFormTester.submit();
            specify(wicket.hasNoErrorMessage().wasFailed(), must.equal(false));
            final List<Competition> list = selectFirst(PropertyListView.class).from(context).getModelObject();
            specify(list.isEmpty(), must.equal(false));
            specify(list.get(0).name, must.equal("jokuKisa"));
        }
    }

}
